<?php 
namespace app\admin\controller;

use app\common\model\CompanyStaff;
use app\common\model\CompanyStaffCommunication;
use app\common\model\CompanyStaffDepart;
use app\common\model\CompanyStaffEducation;
use app\common\model\CompanyStaffOther;
use app\common\model\CompanyStaffTraining;
use think\Db;
use think\Exception;
use think\Loader;
use app\common\model\CompanyDepartment;
use app\common\model\CompanyJobpost;

class Staff extends Base
{
	/**
	 * 添加员工
	 * @throws Exception
	 */
	public function addStaff()
	{
		$data = $this->request->param('');
		Db::startTrans();
		try {
			//验证员工基本信息的合法性
			$validate = Loader::validate('Staff');
			$validate_res = $validate->scene('add')->check($data['base']);
			if(!$validate_res)throw new Exception($validate->getError());
			//验证员工部们信息的合法性
			if(!$data['depart']['depart_id'])throw new Exception('请选择部门');
			if(!$data['depart']['post_id'])throw new Exception('请选择岗位');
			$validate = Loader::validate('Staffdepart');
			$validate_res = $validate->scene('add')->check($data['depart']);
			if(!$validate_res)throw new Exception($validate->getError());
			//验证员工通讯信息合法性
			$validate = Loader::validate('communication');
			$validate_res = $validate->scene('add')->check($data['communication']);
			if(!$validate_res)throw new Exception($validate->getError());
			//验证员工教育信息合法性
			$validate = Loader::validate('education');
			$validate_res = $validate->scene('add')->check($data['education']);
			if(!$validate_res)throw new Exception($validate->getError());
			//添加员工基本信息
			$staff = CompanyStaff::create($data['base']);
			$staff_id = $staff->id;
			//添加员工部门信息
			$data['depart']['staff_id'] = $staff_id;
			CompanyStaffDepart::create($data['depart']);
			//添加员工通讯信息
			$data['communication']['staff_id'] = $staff_id;
			CompanyStaffCommunication::create($data['communication']);
			//添加员工教育信息
			$data['education']['staff_id'] = $staff_id;
			CompanyStaffEducation::create($data['education']);
			//添加员工培训信息
			$data['training']['staff_id'] = $staff_id;
			CompanyStaffTraining::create($data['training']);
			//添加员工其他信息
			$data['other']['staff_id'] = $staff_id;
			CompanyStaffOther::create($data['other']);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return $this->error($e->getMessage());
		}
		return $this->success('员工添加成功');
	}
	
	/**
	 * 修改员工信息
	 * @throws Exception
	 */
	public function editStaff()
	{
		$data = $this->request->param('');
		Db::startTrans();
		try {
			$staff_id = $data['base']['id'];
			//验证员工基本信息的合法性
			$validate = Loader::validate('Staff');
			$validate_res = $validate->scene('add')->check($data['base']);
			if(!$validate_res)throw new Exception($validate->getError());
			//验证员工部们信息的合法性
			if(!$data['depart']['depart_id'])throw new Exception('请选择部门');
			if(!$data['depart']['post_id'])throw new Exception('请选择岗位');
			$validate = Loader::validate('Staffdepart');
			$validate_res = $validate->scene('add')->check($data['depart']);
			if(!$validate_res)throw new Exception($validate->getError());
			//验证员工通讯信息合法性
			$validate = Loader::validate('communication');
			$validate_res = $validate->scene('add')->check($data['communication']);
			if(!$validate_res)throw new Exception($validate->getError());
			//验证员工教育信息合法性
			$validate = Loader::validate('education');
			$validate_res = $validate->scene('add')->check($data['education']);
			if(!$validate_res)throw new Exception($validate->getError());
			//修改员工基本信息
			$staff = CompanyStaff::get($staff_id)->save($data['base']);
			//修改员工部门信息
			$data['depart']['leave_time'] = $data['depart']['leave_time'] ? strtotime($data['depart']['leave_time']) : '';
			$data['depart']['entry_time'] = strtotime($data['depart']['entry_time']);
			CompanyStaffDepart::where(['staff_id' => $staff_id])->update($data['depart']);
			//修改员工通讯信息
			CompanyStaffCommunication::where(['staff_id' => $staff_id])->update($data['communication']);
			//修改员工教育信息
			$data['education']['admission_time'] = strtotime($data['education']['admission_time']);
			$data['education']['graduation_time'] = strtotime($data['education']['graduation_time']);
			CompanyStaffEducation::where(['staff_id' => $staff_id])->update($data['education']);
			//修改员工培训信息
			$data['training']['start_time'] = $data['training']['start_time'] ? strtotime($data['training']['start_time']) : '';
			$data['training']['end_time'] = $data['training']['end_time'] ? strtotime($data['training']['end_time']) : '';
			CompanyStaffTraining::where(['staff_id' => $staff_id])->update($data['training']);
			//修改员工其他信息
			CompanyStaffOther::where(['staff_id' => $staff_id])->update($data['other']);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return $this->error($e->getMessage());
		}
		return $this->success('员工修改成功');
	}
	/**
	 * 获取员工列表
	 */
	public function staffList()
	{
		//获取客户端传递的数据
		$data = $this->request->param('');
		//初始化列表查询条件
		$where = ['state' => 1];
		//重组在职、离职查询条件
		if($data['leave_state'] >= 0) {
			$where['leave_state'] = $data['leave_state'];
		}
		if($data['name']) $where['name'] = $data['name'];
		if($data['times']) {
			$temp_where = [
					'entry_time' => ['between',[strtotime($data['times'][0]),strtotime($data['times'][1])]]
			];
			$staff_id = CompanyStaffDepart::where($temp_where)->column('staff_id');
			$where['id'] = ['in',$staff_id];
		}
		//获取员工基本信息
		$staff_base = CompanyStaff::where($where)->order('leave_state desc,id desc')->field('id,name,sex,nation,political,birth,leave_state')->paginate()->toArray();
		foreach ($staff_base['data'] as &$rows) {
			//获取员工部门信息
			$rows['depart'] = CompanyStaffDepart::where(['staff_id' => $rows['id']])->field('id,depart_id,post_id,entry_time,leave_time,no,salary')->find()->append(['depart_name','post_name'])->toArray();
			$rows['mobile'] = CompanyStaffCommunication::where(['staff_id' => $rows['id']])->value('mobile');
			$rows['education'] = CompanyStaffEducation::where(['staff_id' => $rows['id']])->value('education');
		}
		return $this->success($staff_base);
	}
	
	/**
	 * 员工离职
	 */
	public function staffLeave()
	{
		$staff_id = $this->request->param('staff_id');
		$leave_reason = $this->request->param('leave_reason');
		Db::startTrans();
		try {
			CompanyStaff::get($staff_id)->save(['leave_state' => 0]);
			CompanyStaffDepart::where(['staff_id' => $staff_id])->update(['leave_time' => time(),'leave_reason' => $leave_reason]);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return $this->error($e->getMessage());
		}
		return $this->success('离职成功');
	}

	/**
	 * 获取员工详情信息
	 */
	public function staffInfo()
	{
		$id = $this->request->param('staff_id');
		//获取员工基本信息
		$staff_base = CompanyStaff::get($id)->toArray();
		//获取员工部门信息
		$staff_depart = CompanyStaffDepart::where(['staff_id' => $id])->find()->toArray();
		//获取员工通讯信息
		$staff_communication = CompanyStaffCommunication::where(['staff_id' => $id])->find()->toArray();
		//获取员工教育信息
		$staff_education = CompanyStaffEducation::where(['staff_id' => $id])->find()->toArray();
		//获取员工培训经历
		$staff_training = CompanyStaffTraining::where(['staff_id' => $id])->find()->toArray();
		//获取员工其他信息
		$staff_other = CompanyStaffOther::where(['staff_id' => $id])->find()->toArray();
		//获取公司部门和岗位信息
		$depart = CompanyDepartment::where(['state' => 1])->select()->toArray();
		$post = CompanyJobpost::where(['state' => 1])->select()->toArray();
		$info = [
				'form' => [
						'base' => $staff_base,
						'depart' => $staff_depart,
						'communication' => $staff_communication,
						'education' => $staff_education,
						'training' => $staff_training,
						'other' => $staff_other
				],
				'depart' => $depart,
				'post' => $post
		];
		return $this->success($info);
		
	}

}